package com.azt.front.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class AccessInShortTest {

	public static void main(String[] args) throws InterruptedException {
		int port = 39999;
		for (int i = 0; i < 1; i++) {
			Thread t1 = new Thread(new OutputRuner(port));
			t1.start();
		}
	}
}

class OutputRuner implements Runnable {

	static int no = 1;
	static String recordS = "\r\n";
	static char packageEnd = (char) 0x1a;
	static char fieldSep = (char) 0x09;

	private int port;

	public OutputRuner(int port) {
		this.port = port;
	}

	public void run() {

		for (int i = 0; i < 1; i++) {
			Socket socket = null;
			try {
				socket = new Socket("121.43.116.103", port);
				InputStream in = new BufferedInputStream(socket
						.getInputStream());
				OutputStream out = new BufferedOutputStream(socket
						.getOutputStream());
				StringBuffer sb = new StringBuffer();
//				sb.append(fillSeqIdLength(no++))// 流水号
//						.append("1")// 成功失败标志 仅适用于响应包
//						.append("HUSRBINF    ")// 服务名
//						.append("13276349957         ")// 业务号码
//						.append("1")// 业务号码类型
//						.append("100860")// 营业员
//						.append("OFFICEID")// 营业点
//						.append("1    ")// 包编号
//						.append("1")// 最后一包标志 1表示无后续包
//						.append("     ");// 错误码

				sb.append("A0010301018981                0000000223000000EB001012017031410311217021428496730      999999                                                                                                    00000000000000000000000000000132601                20170314103112999999                                          000000101EB00117021428496730      898111005109404201&1&N&888800117401213&5C7F30C6F73941DF&11.00&0.00&RMB&大家快来发17031319092085840913177&567&None&&");
//				sb.append("123123");
				String msgtemp = "0" + packageEnd;

				// sb.append(fillSeqIdLength(no++))// 流水号
				// .append("1")// 成功失败标志 仅适用于响应包
				// .append("TF_F_USER ")// 服务名
				// .append("13276349957 ")// 业务号码
				// .append("1")// 业务号码类型
				// .append("100860")// 营业员
				// .append("OFFICEID")// 营业点
				// .append("1 ")// 包编号
				// .append("1")// 最后一包标志 1表示无后续包
				// .append(" ");// 错误码
				//
				// String msgtemp = "CT00" + "2004-02-28" + "2004-02-29" +
				// packageEnd;

				// StringBuffer sb = new StringBuffer();
				// sb.append(fillSeqIdLength(no++))// 流水号
				// .append("1")// 成功失败标志 仅适用于响应包
				// .append("H2QDCX ")// 服务名
				// .append("13574851218 ")// 业务号码
				// .append("1")// 业务号码类型
				// .append("100860")// 营业员
				// .append("OFFICEID")// 营业点
				// .append("1 ")// 包编号
				// .append("1")// 最后一包标志 1表示无后续包
				// .append("12345");// 错误码
				//
				// String msgtemp = "08200808012008083113574851218 % %%0"
				// + packageEnd;

				byte[] temp = msgtemp.getBytes("UTF-8");
				int lengthReal = temp.length + 86;
				String lengthStr = fillTotalLength(lengthReal);
				String msg = "10" + lengthStr + sb.toString() + msgtemp;
//				System.out.println("消息" + msg);
				msg = sb.toString();
				byte[] real = msg.getBytes("gbk");
				out.write(real);
				out.flush();

				byte[] head = new byte[7];
				int readed = 0;
				while (readed < 7) {
					readed += in.read(head, readed, 7 - readed);
				}
				byte[] lengthArr = new byte[5];
				for (int j = 0; j < 5; j++) {
					lengthArr[j] = head[j + 2];
				}
				String lenStr = new String(lengthArr);
				int len = Integer.parseInt(lenStr.trim());
				byte[] content = new byte[len];
				for (int j = 0; j < 7; j++) {
					content[j] = head[j];
				}
				while (readed < len) {
					readed += in.read(content, readed, len - readed);
				}
				System.out.println(new String(content, "UTF-8"));

				// int tempRead = 0;
				// while ((tempRead = in.read()) != -1 && tempRead != (byte)
				// 0x1a) {
				// char tempChar = (char) tempRead;
				// System.out.print(tempChar);
				// }
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					if (socket != null)
						socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * 流水号填充长度20位
	 */
	private String fillSeqIdLength(int no) {
		StringBuffer sb = null;
		String lengthStr = Integer.toString(no);
		if (lengthStr.length() > 20) {
			return null;
		} else {
			sb = new StringBuffer();
			for (int i = 0; i < 20 - lengthStr.length(); i++) {
				sb.append("0");
			}
			sb.append(lengthStr);
		}
		return sb.toString();
	}

	/**
	 * 填充消息总长度(A1-A0位)
	 */
	String fillTotalLength(int length) {
		StringBuffer sb = null;
		String lengthStr = Integer.toString(length);
		if (lengthStr.length() > 5) {
			return null;
		} else {
			sb = new StringBuffer(lengthStr);
			for (int i = 0; i < 5 - lengthStr.length(); i++) {
				sb.append(" ");
			}
		}
		return sb.toString();
	}
}

class InputRuner implements Runnable {

	private InputStream in;

	public InputRuner(InputStream in) {
		this.in = in;
	}

	public void run() {
		try {
			int tempRead = 0;
			while ((tempRead = in.read()) != -1 && tempRead != (byte) 0x1a) {
				char tempChar = (char) tempRead;
				// ReturnMessage += tempChar;
				System.out.print(tempChar);
				// Thread.sleep(50);
			}
		} catch (Exception e) {

		}

	}

}
